import axios from "axios"
import {ElMessage} from "element-plus"

const instance = axios.create({
    baseURL: 'http://127.0.0.1:8000/api',
    timeout: 5000
})
//请求拦截器
instance.interceptors.request.use(config => {
    //在请求api之前携带token
    const token = window.sessionStorage.getItem('token')
    if (token) {
        config.headers = {
            'Authorization': 'Token ' + token
        }
    }
    return config;
}, error => {
    return Promise.reject(error)
})
//响应拦截器
instance.interceptors.response.use(response => {
    //处理响应数据
    if (response.data.code != 200) {
        ElMessage.error(response.data.msg)
    }
    return response
}, error => {
    //处理catch的地方
    ElMessage('连接服务器失败，请稍后再试！！')
    return Promise.reject(error)
})

export default instance